import { IJewelExchangeOper } from "./IJewelExchangeOper";
import { JewelExchangeOperateItem } from "./JewelExchangeOperateItem";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Language } from "../../../../../extend/language/Language";
import { ItemData } from "../../../resource/info/ItemData";
import { Cache } from "../../../cache/Cache";
import { SmeltConfig } from "../../../resource/SmeltConfig";
import { DataEvent } from "../../../events/DataEvent";
import { EquipmentModuleConst } from "../../equipment/data/EquipmentModuleConst";
import { Vector } from "../../../../../__AS3__/vec/Vector";
type int = number;
//class JewelExchangeNormal
    
    export  class JewelExchangeNormal /* flash.display.Sprite */ implements IJewelExchangeOper
    {
        constructor()
        {
            
            this._operaItems = new Vector.<JewelExchangeOperateItem>();
            this.createChildren();
            return;
        }

        protected createChildren(): void
        {
            var loc1=null;
            ObjCreate.createBitmap(ImagesConst.Fire, 197, -32, this);
            ObjCreate.createLabel(Language.getString(69232), 220, -25, 186, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            ObjCreate.createLabel(Language.getString(69217), 0, 8, 220, 20, flash.text.TextFormatAlign.CENTER, this, false, 16776960);
            ObjCreate.createLabel(Language.getString(69218), 190, 8, 220, 20, flash.text.TextFormatAlign.CENTER, this, false, 16776960);
            var loc2=0;
            while (loc2 < 5) 
            {
                loc1 = new JewelExchangeOperateItem();
                loc1.x = 12;
                loc1.y = 33 + 65 * loc2;
                loc1.addEventListener(JewelExchangeOperateItem.MoveOutJewel, this.onJewelMoveOut);
                this.addChild(loc1);
                this._operaItems.push(loc1);
                ++loc2;
            }
            return;
        }

        public addJewel(arg1: ItemData): void
        {
            var loc1=this.getNullItem();
            if (loc1) 
            {
                loc1.setJewelData(arg1, true);
            }
            return;
        }

        public removeJewel(arg1: ItemData): void
        {
            var loc1=this.getItemByUid(arg1.uid);
            if (loc1) 
            {
                loc1.setJewelData(null);
            }
            this.updateSequence();
            return;
        }

        public checkJewelLost(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._operaItems;
            for(loc1 of loc3) 
            {
                if (!(loc1.jewel.itemData && this.isLost(loc1.jewel.itemData))) 
                {
                    continue;
                }
                loc1.setJewelData(null);
            }
            return;
        }

        /* internal  */isLost(arg1: ItemData): Boolean
        {
            var loc1=Cache.instance.pack.stonePackCache.getItemDataByUid(arg1.uid);
            if (loc1 && loc1.itemAmount == arg1.itemAmount) 
            {
                return false;
            }
            return true;
        }

        public hasFullPutIn(): Boolean
        {
            if (this.getNullItem()) 
            {
                return false;
            }
            return true;
        }

        public clearJewels(): void
        {
            var loc1=0;
            while (loc1 < this._operaItems.length) 
            {
                this._operaItems[loc1].setJewelData(null);
                ++loc1;
            }
            return;
        }

        public getCost(): int
        {
            var loc1=0;
            var loc2=null;
            var loc3=0;
            var loc4=this._operaItems;
            for(loc2 of loc4) 
            {
                if (!loc2.jewel.itemData) 
                {
                    continue;
                }
                loc1 = loc1 + SmeltConfig.instance.getJewelTransCostByCode(loc2.jewel.itemData.itemCode) * loc2.currentNum;
            }
            return loc1;
        }

        public hasJewelPutIn(): Boolean
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._operaItems;
            for(loc1 of loc3) 
            {
                if (!loc1.jewel.itemData) 
                {
                    continue;
                }
                return true;
            }
            return false;
        }

        /* internal  */onJewelMoveOut(arg1: Object /* flash.events.Event */): void
        {
            var loc1=arg1.target as JewelExchangeOperateItem;
            loc1.setJewelData(null);
            this.updateSequence();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_JewelExchangeUpdateJewels, null, false, true, true));
            return;
        }

        public updateSequence(): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            var loc4=0;
            var loc5=0;
            loc1 = 0;
            while (loc1 < this._operaItems.length) 
            {
                if (!this._operaItems[loc1].isPutInJewel()) 
                {
                    loc2 = loc1 + 1;
                    while (loc2 < this._operaItems.length) 
                    {
                        if (this._operaItems[loc2].isPutInJewel()) 
                        {
                            loc3 = this._operaItems[loc2].jewel.itemData;
                            loc4 = this._operaItems[loc2].currentNum;
                            loc5 = this._operaItems[loc2].currentTransType;
                            this._operaItems[loc2].setJewelData(null);
                            this._operaItems[loc1].setJewelData(loc3);
                            this._operaItems[loc1].setTransNum(loc4);
                            this._operaItems[loc1].setTransType(loc5);
                        }
                        ++loc2;
                    }
                }
                ++loc1;
            }
            return;
        }

        public getNullItem(): JewelExchangeOperateItem
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._operaItems;
            for(loc1 of loc3) 
            {
                if (loc1.isPutInJewel() != false) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        public getItemByUid(arg1: string): JewelExchangeOperateItem
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._operaItems;
            for(loc1 of loc3) 
            {
                if (!(loc1.jewel.itemData && loc1.jewel.itemData.uid == arg1)) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        public getTransJewelDic(): Map<any, any> /* flash.utils.Dictionary */
        {
            var loc2=null;
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc3=0;
            var loc4=this._operaItems;
            for(loc2 of loc4) 
            {
                if (!loc2.jewel.itemData) 
                {
                    continue;
                }
                loc1[loc2.jewel.itemData.uid] = loc2.currentNum;
            }
            return loc1;
        }

        public getTargetJewels(): Map<any, any> /* flash.utils.Dictionary */
        {
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc2=0;
            while (loc2 < this._operaItems.length) 
            {
                if (this._operaItems[loc2].isPutInJewel()) 
                {
                    loc1[this._operaItems[loc2].jewel.itemData.uid] = this._operaItems[loc2].jewelNew.itemData.itemCode;
                }
                ++loc2;
            }
            return loc1;
        }

        /* internal */ /* var */_operaItems: Vector.<JewelExchangeOperateItem>;
    }
